package com.funny.learning.english.api;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONObject;

import android.content.Context;
import android.util.Log;
import android.widget.Toast;

import com.androidquery.AQuery;
import com.androidquery.callback.AjaxCallback;
import com.androidquery.callback.AjaxStatus; 
import com.funny.learning.english.api.BaseAjaxCallBack.IAjaxCallbackMMK; 
import com.funny.learning.english.model.CourseEntity;
import com.funny.learning.english.model.GameEntity;
import com.funny.learning.english.model.LanguageEntity;
import com.funny.learning.english.model.TopicEntity;
import com.funny.learning.english.model.UserEntity;
import com.funny.learning.english.util.DeviceUtil;
import com.funny.learning.english.util.PreferenceShUtil;

public class TiengAnhApi {

	private static final String TAG = "TiengAnhApi";

	public static void register(String name, String pwd, String email,
			Context ctx, final IAjaxCallbackMMK callback) {
		AQuery aQuery = new AQuery(ctx);

		BaseAjaxCallBack<String> ajaxCallback = new BaseAjaxCallBack<String>() {

			@Override
			public void async(Context context) {
				// TODO Auto-generated method stub 
				super.async(context);
				callback.handler(null, IAjaxCallbackMMK.START);
				Log.d(TAG, "START");
			}

			@Override
			public void callback(String url, String object, AjaxStatus status) {
				// TODO Auto-generated method stub
				super.callback(url, object, status);

				if (null != object) {
					Log.d(TAG, object.toString());
					try {
						JSONObject jsonObject = new JSONObject(object);
						boolean success = jsonObject.optBoolean("success",
								false);
						if (!success) {
							callback.handler(null, IAjaxCallbackMMK.ERROR);
						} else {
							JSONObject jsonUser = jsonObject
									.getJSONObject("result");
							JSONObject jObjCoin = jsonObject.getJSONObject("coin");
							UserEntity entity = new UserEntity(jsonUser, jObjCoin);
							callback.handler(entity, IAjaxCallbackMMK.COMPLETE);
						}

					} catch (Exception e) {
						// TODO: handle exception
						e.printStackTrace();
						callback.handler(null, IAjaxCallbackMMK.ERROR);
					}

				} else {
					callback.handler(null, IAjaxCallbackMMK.ERROR);
					Log.d(TAG, "null");
				}

			}
		};

		String url = Define.API_REGISTER + "?name=" + name + "&pass=" + pwd
				+ "&mail=" + email + Define.PREFIX
				+ DeviceUtil.getDeviceId(ctx);

		Log.d(TAG, url);

		// ajaxCallback.cookies(sCookies);
		aQuery.ajax(url, String.class, ajaxCallback);
	}

	public static void loginWithFacebook(String access_token, Context ctx,
			final IAjaxCallbackMMK callback) {
		AQuery aQuery = new AQuery(ctx);

		BaseAjaxCallBack<String> ajaxCallback = new BaseAjaxCallBack<String>() {

			@Override
			public void async(Context context) {
				// TODO Auto-generated method stub
				super.async(context);
				callback.handler(null, IAjaxCallbackMMK.START);
				Log.d(TAG, "START");
			}

			@Override
			public void callback(String url, String object, AjaxStatus status) {
				// TODO Auto-generated method stub
				super.callback(url, object, status);

				if (null != object) {
					Log.d(TAG, object.toString());
					try {
						JSONObject jsonObject = new JSONObject(object);
						boolean success = jsonObject.optBoolean("success",
								false);
						if (!success) {
							callback.handler(null, IAjaxCallbackMMK.ERROR);
						} else {
							JSONObject jsonUser = jsonObject
									.getJSONObject("result");
							JSONObject jObjCoin = jsonObject.getJSONObject("coin");
							UserEntity entity = new UserEntity(jsonUser, jObjCoin);
							callback.handler(entity, IAjaxCallbackMMK.COMPLETE);
						}

					} catch (Exception e) {
						// TODO: handle exception
						e.printStackTrace();
						callback.handler(null, IAjaxCallbackMMK.ERROR);
					}

				} else {
					callback.handler(null, IAjaxCallbackMMK.ERROR);
					Log.d(TAG, "null");
				}

			}
		};

		String url = Define.API_LOGIN_WITH_FACEBOOK + access_token
				+ Define.PREFIX + DeviceUtil.getDeviceId(ctx);

		Log.d(TAG, url);

		// ajaxCallback.cookies(sCookies);
		aQuery.ajax(url, String.class, ajaxCallback);

		// aQuery.download(url, ctx.MODE_WORLD_READABLE, cb)
	}

	public static void login(String name, String pwd, Context ctx,
			final IAjaxCallbackMMK callback) {
		AQuery aQuery = new AQuery(ctx);

		BaseAjaxCallBack<String> ajaxCallback = new BaseAjaxCallBack<String>() {

			@Override
			public void async(Context context) {
				// TODO Auto-generated method stub
				sCookies.clear();
				super.async(context);
				callback.handler(null, IAjaxCallbackMMK.START);
				Log.d(TAG, "START");
			}

			@Override
			public void callback(String url, String object, AjaxStatus status) {
				// TODO Auto-generated method stub
				super.callback(url, object, status);

				if (null != object) {
					Log.d(TAG, object.toString());
					try {
						JSONObject jsonObject = new JSONObject(object);
						boolean success = jsonObject.optBoolean("success",
								false);
						if (!success) {
							callback.handler(null, IAjaxCallbackMMK.ERROR);
						} else {
//							JSONObject jsonUser = jsonObject.getJSONObject("result");
							callback.handler(object, IAjaxCallbackMMK.COMPLETE);
						}

					} catch (Exception e) {
						// TODO: handle exception
						e.printStackTrace();
						callback.handler(null, IAjaxCallbackMMK.ERROR);
					}

				} else {
					callback.handler(null, IAjaxCallbackMMK.ERROR);
					Log.d(TAG, "null");
				}

			}
		};

		String url = Define.API_LOGIN + "?lname=" + name + "&pass=" + pwd
				+ "&remember=1" + Define.PREFIX + DeviceUtil.getDeviceId(ctx);

		Log.d(TAG, url);

		// ajaxCallback.cookies(sCookies);
		aQuery.ajax(url, String.class, ajaxCallback);
	}

	public static void getForgotPass(String email, String code, String pass,
			String confrimPass, Context ctx, final IAjaxCallbackMMK callback) {
		AQuery aQuery = new AQuery(ctx);

		BaseAjaxCallBack<String> ajaxCallback = new BaseAjaxCallBack<String>() {

			@Override
			public void async(Context context) {
				// TODO Auto-generated method stub
				super.async(context);
				callback.handler(null, IAjaxCallbackMMK.START);
				Log.d(TAG, "START");
			}

			@Override
			public void callback(String url, String object, AjaxStatus status) {
				// TODO Auto-generated method stub
				super.callback(url, object, status);

				if (null != object) {
					Log.d(TAG, object.toString());
					try {
						JSONObject jsonObject = new JSONObject(object);
						boolean success = jsonObject.optBoolean("success",
								false);
						if (!success) {
							callback.handler(null, IAjaxCallbackMMK.ERROR);
						} else {
							callback.handler(null, IAjaxCallbackMMK.COMPLETE);
						}

					} catch (Exception e) {
						// TODO: handle exception
						e.printStackTrace();
						callback.handler(null, IAjaxCallbackMMK.ERROR);
					}

				} else {
					callback.handler(null, IAjaxCallbackMMK.ERROR);
					Log.d(TAG, "null");
				}

			}
		};

		String url = Define.API_GET_PASS_FORGOT + "?mail=" + email + "&code="
				+ code + "&pass=" + pass + "&confirm_pass=" + confrimPass
				+ Define.PREFIX + DeviceUtil.getDeviceId(ctx);

		Log.d(TAG, url);

		aQuery.ajax(url, String.class, ajaxCallback);
	}

	public static void forgotPass(String email, Context ctx,
			final IAjaxCallbackMMK callback) {
		AQuery aQuery = new AQuery(ctx);

		BaseAjaxCallBack<String> ajaxCallback = new BaseAjaxCallBack<String>() {

			@Override
			public void async(Context context) {
				// TODO Auto-generated method stub
				super.async(context);
				callback.handler(null, IAjaxCallbackMMK.START);
				Log.d(TAG, "START");
			}

			@Override
			public void callback(String url, String object, AjaxStatus status) {
				// TODO Auto-generated method stub
				super.callback(url, object, status);
				if (null != object) {
					Log.d(TAG, object.toString());
					try {
						JSONObject jsonObject = new JSONObject(object);
						boolean success = jsonObject.getBoolean("success");

						Log.d(TAG, "success:::" + success);
						if (!success) {
							callback.handler(null, IAjaxCallbackMMK.ERROR);
						} else {
							callback.handler(null, IAjaxCallbackMMK.COMPLETE);
						}
					} catch (Exception e) {
						// TODO: handle exception
						e.printStackTrace();
						callback.handler(null, IAjaxCallbackMMK.ERROR);
					}

				} else {
					callback.handler(null, IAjaxCallbackMMK.ERROR);
					Log.d(TAG, "null");
				}

			}
		};

		String url = Define.API_FORGOT_PASS + "?mail=" + email + Define.PREFIX
				+ DeviceUtil.getDeviceId(ctx);

		Log.d(TAG, url);

		aQuery.ajax(url, String.class, ajaxCallback);
	}
	
	public static void logout(Context ctx,
			final IAjaxCallbackMMK callback) {
		AQuery aQuery = new AQuery(ctx);

		BaseAjaxCallBack<String> ajaxCallback = new BaseAjaxCallBack<String>() {

			@Override
			public void async(Context context) {
				// TODO Auto-generated method stub
				super.async(context);
				callback.handler(null, IAjaxCallbackMMK.START);
				Log.d(TAG, "START");
			}

			@Override
			public void callback(String url, String object, AjaxStatus status) {
				// TODO Auto-generated method stub
				super.callback(url, object, status);
				if (null != object) {
					Log.d(TAG, object.toString());
					try {
						JSONObject jsonObject = new JSONObject(object);
						boolean success = jsonObject.getBoolean("success");

						Log.d(TAG, "success:::" + success);
						if (!success) {
							callback.handler(null, IAjaxCallbackMMK.ERROR);
						} else {
							callback.handler(null, IAjaxCallbackMMK.COMPLETE);
						}
					} catch (Exception e) {
						// TODO: handle exception
						e.printStackTrace();
						callback.handler(null, IAjaxCallbackMMK.ERROR);
					}

				} else {
					callback.handler(null, IAjaxCallbackMMK.ERROR);
					Log.d(TAG, "null");
				}

			}
		};

		String url = Define.API_LOGOUT + Define.PREFIX
				+ DeviceUtil.getDeviceId(ctx);

		Log.d(TAG, url);

		aQuery.ajax(url, String.class, ajaxCallback);
	}

	public static void resetPass(String id, String oldPass, String newPass,
			Context ctx, final IAjaxCallbackMMK callback) {
		AQuery aQuery = new AQuery(ctx);

		BaseAjaxCallBack<String> ajaxCallback = new BaseAjaxCallBack<String>() {

			@Override
			public void async(Context context) {
				// TODO Auto-generated method stub
				super.async(context);
				callback.handler(null, IAjaxCallbackMMK.START);
				Log.d(TAG, "START");
			}

			@Override
			public void callback(String url, String object, AjaxStatus status) {
				// TODO Auto-generated method stub
				super.callback(url, object, status);

				if (null != object) {
					Log.d(TAG, object.toString());
					try {
						JSONObject jsonObject = new JSONObject(object);
						boolean success = jsonObject.optBoolean("success",
								false);
						if (!success) {
							callback.handler(null, IAjaxCallbackMMK.ERROR);
						} else {
							callback.handler(null, IAjaxCallbackMMK.COMPLETE);
						}

					} catch (Exception e) {
						// TODO: handle exception
						e.printStackTrace();
						callback.handler(null, IAjaxCallbackMMK.ERROR);
					}

				} else {
					callback.handler(null, IAjaxCallbackMMK.ERROR);
					Log.d(TAG, "null");
				}

			}
		};

		String url = Define.API_RESET_PASS + "?id=" + id + "&old_pass="
				+ oldPass + "&pass=" + newPass + "&_cl_step=change_pass"
				+ Define.PREFIX + DeviceUtil.getDeviceId(ctx);

		Log.d(TAG, url);

		aQuery.ajax(url, String.class, ajaxCallback);
	}

	public static void registerCourse(String idc, String hl, String type,
			int[] level, Context ctx, final IAjaxCallbackMMK callback) {
		AQuery aQuery = new AQuery(ctx);

		BaseAjaxCallBack<String> ajaxCallback = new BaseAjaxCallBack<String>() {

			@Override
			public void async(Context context) {
				// TODO Auto-generated method stub
				super.async(context);
				callback.handler(null, IAjaxCallbackMMK.START);
				Log.d(TAG, "START");
			}

			@Override
			public void callback(String url, String object, AjaxStatus status) {
				// TODO Auto-generated method stub
				super.callback(url, object, status);

				if (null != object) {
					Log.d(TAG, object.toString());
					try {
						JSONObject jsonObject = new JSONObject(object);
						boolean success = jsonObject.optBoolean("success",
								false);
						if (!success) {
							String message = jsonObject.getString("err");
							callback.handler(message, IAjaxCallbackMMK.ERROR);
						} else {

							callback.handler(null, IAjaxCallbackMMK.COMPLETE);
						}

					} catch (Exception e) {
						// TODO: handle exception
						e.printStackTrace();
						callback.handler(null, IAjaxCallbackMMK.ERROR);
					}

				} else {
					callback.handler(null, IAjaxCallbackMMK.ERROR);
					Log.d(TAG, "null");
				}

			}
		};

		String levels = "";

		for (int i = 0; i < level.length; ++i) {
			levels = levels + "&lv[" + level[i] + "]=" + level[i];
		}

		String url = Define.API_REGISTER_COURSE + "?idc=" + idc + "&hl=" + hl
				+ levels + "&type=" + type + Define.PREFIX
				+ DeviceUtil.getDeviceId(ctx);

		Log.d(TAG, url);

		aQuery.ajax(url, String.class, ajaxCallback);
	}

	public static void registerLanguage(String hl, UserEntity userEntity,
			Context ctx, final IAjaxCallbackMMK callback) {
		AQuery aQuery = new AQuery(ctx);

		BaseAjaxCallBack<String> ajaxCallback = new BaseAjaxCallBack<String>() {

			@Override
			public void async(Context context) {
				// TODO Auto-generated method stub
				super.async(context);
				callback.handler(null, IAjaxCallbackMMK.START);
				Log.d(TAG, "START");
			}

			@Override
			public void callback(String url, String object, AjaxStatus status) {
				// TODO Auto-generated method stub
				super.callback(url, object, status);

				if (null != object) {
					Log.d(TAG, object.toString());
					try {
						JSONObject jsonObject = new JSONObject(object);
						boolean success = jsonObject.optBoolean("success",
								false);
						if (!success) {
							String message = jsonObject.getString("err");
							callback.handler(message, IAjaxCallbackMMK.ERROR);
						} else {

							callback.handler(null, IAjaxCallbackMMK.COMPLETE);
						}

					} catch (Exception e) {
						// TODO: handle exception
						e.printStackTrace();
						callback.handler(null, IAjaxCallbackMMK.ERROR);
					}

				} else {
					callback.handler(null, IAjaxCallbackMMK.ERROR);
					Log.d(TAG, "null");
				}

			}
		};

		String url = Define.API_REGISTER_LANGUAGE + "?hl=" + hl + "&_cl_uhash="
				+ userEntity.getUhash() + "&_cl_token=" + userEntity.getToken()
				+ Define.PREFIX1 + DeviceUtil.getDeviceId(ctx);

		Log.d(TAG, url);

		aQuery.ajax(url, String.class, ajaxCallback);
	}

	public static void getCourseList(final Context ctx, UserEntity userEntity,
			String hl, final IAjaxCallbackMMK callback) {
		AQuery aQuery = new AQuery(ctx);

		BaseAjaxCallBack<String> ajaxCallback = new BaseAjaxCallBack<String>() {

			@Override
			public void async(Context context) {
				// TODO Auto-generated method stub
				super.async(context);
				callback.handler(null, IAjaxCallbackMMK.START);
				Log.d(TAG, "START");
			}

			@Override
			public void callback(String url, String object, AjaxStatus status) {
				// TODO Auto-generated method stub
				super.callback(url, object, status);

				if (null != object) {
					Log.d(TAG, object.toString());
					try {
						JSONObject jsonObject = new JSONObject(object);
						boolean success = jsonObject.optBoolean("success",
								false);
						if (!success) {
							callback.handler(null, IAjaxCallbackMMK.ERROR);
						} else {
							ArrayList<CourseEntity> courseEntitys = new ArrayList<CourseEntity>();
							JSONArray jsonArray = jsonObject
									.getJSONArray("result");
							
							saveFile(jsonArray.toString(), ctx.getFilesDir() + "/getCourseList.txt");
							
							PreferenceShUtil.setValue(ctx, PreferenceShUtil.KEY_LIST_course, jsonArray.toString());

							for (int i = 0; i < jsonArray.length(); ++i) {
								JSONObject jsonObject2 = jsonArray
										.getJSONObject(i);
								courseEntitys
										.add(new CourseEntity(jsonObject2));
							}

							callback.handler(courseEntitys,
									IAjaxCallbackMMK.COMPLETE);
						}

					} catch (Exception e) {
						// TODO: handle exception
						e.printStackTrace();
						callback.handler(null, IAjaxCallbackMMK.ERROR);
					}

				} else {
					callback.handler(null, IAjaxCallbackMMK.ERROR);
					Log.d(TAG, "null");
				}

			}
		};

		String url = Define.API_COURSE + "?hl=" + hl + "&_cl_uhash="
				+ userEntity.getUhash() + "&_cl_token=" + userEntity.getToken()
				+ Define.PREFIX1 + DeviceUtil.getDeviceId(ctx);

		Log.d(TAG, url);

		aQuery.ajax(url, String.class, ajaxCallback);
	}

	public static void getLanguageList(final Context ctx,
			final IAjaxCallbackMMK callback) {
		AQuery aQuery = new AQuery(ctx);

		BaseAjaxCallBack<String> ajaxCallback = new BaseAjaxCallBack<String>() {

			@Override
			public void async(Context context) {
				// TODO Auto-generated method stub
				super.async(context);
				callback.handler(null, IAjaxCallbackMMK.START);
				Log.d(TAG, "START");
			}

			@Override
			public void callback(String url, String object, AjaxStatus status) {
				// TODO Auto-generated method stub
				super.callback(url, object, status);

				if (null != object) {
					Log.d(TAG, object.toString());
					try {
						JSONObject jsonObject = new JSONObject(object);
						boolean success = jsonObject.optBoolean("success",
								false);
						if (!success) {
							callback.handler(null, IAjaxCallbackMMK.ERROR);
						} else {
							ArrayList<LanguageEntity> languageEntities = new ArrayList<LanguageEntity>();
							JSONArray jsonArray = jsonObject
									.getJSONArray("result");
							PreferenceShUtil.setValue(ctx, PreferenceShUtil.KEY_LIST_LANG, jsonArray.toString());

							for (int i = 0; i < jsonArray.length(); ++i) {
								JSONObject jsonObject2 = jsonArray
										.getJSONObject(i);
								languageEntities.add(new LanguageEntity(
										jsonObject2));
							}

							callback.handler(languageEntities,
									IAjaxCallbackMMK.COMPLETE);
						}

					} catch (Exception e) {
						// TODO: handle exception
						e.printStackTrace();
						callback.handler(null, IAjaxCallbackMMK.ERROR);
					}

				} else {
					callback.handler(null, IAjaxCallbackMMK.ERROR);
					Log.d(TAG, "null");
				}

			}
		};

		String url = Define.API_LANGUAGE + "?" + Define.PREFIX
				+ DeviceUtil.getDeviceId(ctx);

		Log.d(TAG, url);

		aQuery.ajax(url, String.class, ajaxCallback);
	}
	

	public static void topicCase3(final Context ctx, final IAjaxCallbackMMK callback) {
		AQuery aQuery = new AQuery(ctx);

		BaseAjaxCallBack<String> ajaxCallback = new BaseAjaxCallBack<String>() {

			@Override
			public void async(Context context) {
				fileCache(true);
				super.async(context);
				callback.handler(null, IAjaxCallbackMMK.START);
				Log.d(TAG, "START");
			}

			@Override
			public void callback(String url, String object, AjaxStatus status) {
				// TODO Auto-generated method stub
				super.callback(url, object, status);

				if (null != object) {
					Log.d(TAG, object.toString());
					// saveFile(object.toString(), "/Kpro/topicCase3.txt");
					try {
						JSONObject jsonObject = new JSONObject(object);
						boolean success = jsonObject.optBoolean("success",
								false);
						if (!success) {
							String message = jsonObject.getString("err");
							callback.handler(message, IAjaxCallbackMMK.ERROR);
						} else {

							ArrayList<TopicEntity> arrTopic = new ArrayList<TopicEntity>();
							JSONArray jsonArray = jsonObject
									.getJSONArray("result");
							
//							saveFile(jsonArray.toString(), ctx.getFilesDir() + "/tuan26.txt");

							for (int i = 0; i < jsonArray.length(); ++i) {
								JSONObject jsonObject2 = jsonArray
										.getJSONObject(i);
								arrTopic.add(new TopicEntity(jsonObject2));
							}
//							String pathData = ctx.getFilesDir().getPath()
//									+ File.separator + "Android" + File.separator + "dulieu";
							PreferenceShUtil.setValue(ctx, PreferenceShUtil.KEY_VALUE_LST_TOP_SUB_GAME, arrTopic.toString());
							callback.handler(arrTopic,
									IAjaxCallbackMMK.COMPLETE);

						}
						// saveFile(jsonObject.toString(),
						// "/Kpro/topicCase3.txt");
					} catch (Exception e) {
						// TODO: handle exception
						e.printStackTrace();
						callback.handler(null, IAjaxCallbackMMK.ERROR);
					}

				} else {
					callback.handler(null, IAjaxCallbackMMK.ERROR);
					Log.d(TAG, "null");
				}

			}
		};
		String url = Define.API_HOME + Define.PREFIX
				+ DeviceUtil.getDeviceId(ctx);

		Log.d(TAG, url);

		aQuery.ajax(url, String.class, ajaxCallback);
	}

	public static void playGame(final Context ctx, final String topic,
			final String subTopic, final String game,
			final IAjaxCallbackMMK callback) {
		AQuery aQuery = new AQuery(ctx);

		BaseAjaxCallBack<String> ajaxCallback = new BaseAjaxCallBack<String>() {

			@Override
			public void async(Context context) {
				// TODO Auto-generated method stub
				super.async(context);
				callback.handler(null, IAjaxCallbackMMK.START);
				Log.d(TAG, "START");
			}

			@Override
			public void callback(String url, String object, AjaxStatus status) {
				// TODO Auto-generated method stub
				super.callback(url, object, status);
				if (null != object) {
					Log.d(TAG, "object::" + object.toString());
					saveFile(object.toString(), ctx.getFilesDir() + "/playGame.txt");
					try {
						JSONObject jsonObject = new JSONObject(object);
						JSONArray jsonArray = new JSONArray();

						boolean success = jsonObject.optBoolean("success",
								false);

						if (!success) {
							String message = jsonObject.getString("err");
							callback.handler(message, IAjaxCallbackMMK.ERROR);

						} else {
							jsonArray = jsonObject.optJSONArray("result");

							ArrayList<GameEntity> gameEntities = new ArrayList<GameEntity>();

							for (int i = 0; i < jsonArray.length(); i++) {
								JSONObject jsObj = jsonArray.optJSONObject(i);

								GameEntity gameEntity = new GameEntity(jsObj,
										topic, subTopic, game);

								Log.d("++++++++", gameEntity.toString());

								gameEntities.add(gameEntity);

							}

							callback.handler(gameEntities,
									IAjaxCallbackMMK.COMPLETE);
						}
					} catch (Exception e) {
						e.printStackTrace();
						callback.handler(null, IAjaxCallbackMMK.ERROR);
						Log.e(TAG, "Parrse Json Sai::");
					}

				} else {
					callback.handler(null, IAjaxCallbackMMK.ERROR);
					Log.e(TAG, "Object rong::");
					Log.e(TAG, "null");
				}

			}
		};
		String url = Define.API_PLAY_GAME + "/" + topic + "/" + subTopic + "/"
				+ game + "?" + Define.PREFIX + DeviceUtil.getDeviceId(ctx);

		Log.d(TAG, url);

		aQuery.ajax(url, String.class, ajaxCallback);
	}

	private static void saveFile(String text, String file) {
		String UTF8 = "utf8";
		int BUFFER_SIZE = 8192;
		FileOutputStream fos;
		char[] c_arr = text.toCharArray();
		try {
			BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(
					new FileOutputStream(file), UTF8), BUFFER_SIZE);
			bw.write(c_arr);
			bw.flush();
			bw.close();
		} catch (FileNotFoundException e) {
		} catch (IOException e) {
		}
		
	}

	public static void downloadData(final Context context, String url,
			String name) {
		AQuery aQuery = new AQuery(context);
		aQuery.download("http://static.vietalo.com/mp3/" + url, new File(
				context.getFilesDir(), "m/" + name + ".mp3"),
				new AjaxCallback<File>() {
					@Override
					public void callback(String url, File object,
							AjaxStatus status) {
						super.callback(url, object, status);
						Log.d(TAG, "Object::" + object.getPath());
						Log.d(TAG, "url::" + url);
						if (object != null) {
							Toast.makeText(context, "Download thanh cong", 1000)
									.show();
						} else {
							Toast.makeText(context, "Download that bai", 1000)
									.show();
						}
					}

				});
	}
}
